package wxdb.parse.absyn;
import wxdb.query.*;
import wxdb.symbol.Symbol;


public class WhereLst extends Exp{
	public WClause wclause;
	public WhereLst WLHead;
	public WhereLst WLTail;
	public BinOper whereOper;
	public WhereLst(int p, WClause wc, WhereLst wlh, WhereLst wlt, BinOper wo){
		pos = p;
		wclause = wc;
		WLHead = wlh;
		WLTail = wlt;
		whereOper = wo;
	}
	
	public WhereLst whereLstHead(){
		return WLHead;
	}
	
	public WhereLst whereLstTail(){
		return WLTail;
	}
	
	public BinOper whereOper(){
		return whereOper;
	}
	
	/*	public Predicate predicate() {
	Predicate pred = new Predicate(term());
	if (lex.matchKeyword("and")) {
		lex.eatKeyword("and");
		pred.conjoinWith(predicate());
	}
	return pred;
}*/

	public Predicate predicate() {
		Predicate pred = new Predicate();
		if(whereOper()!=null){
			if(whereOper().isAnd()){
				pred = WLHead.predicate();
				pred.conjoinWith(WLTail.predicate(),"and");
			}else if(whereOper().isOr()){
				pred = WLHead.predicate();
				pred.conjoinWith(WLTail.predicate(),"or");
			}
		}else{
			pred = new Predicate(term());
		}
		return pred;
	}
	
	public Term term() {
		Expression lhs = new FieldNameExpression(wclause.fieldName());
		BinOper binOper = wclause.binOper();
		RValExp rve =  ((RValExprs)wclause.RValue()).exp; 
		ValExp ve = (ValExp)rve;
		Val v = ve.val;
		
		Constant value;
		if(v instanceof IntVal){
			value = new IntConstant(((IntVal) v).ival);
		}else if(v instanceof FloatVal){
			value = new FloatConstant(((FloatVal) v).fltval);
		}else{
			value = new StringConstant(((StringVal) v).sval);
		}
		Expression rhs = new ConstantExpression(value);
		return new Term(lhs, rhs, binOper);
	}
}
